<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>
            <span>姓名:</span>
            <input type="text" v-model="name" />
        </div>
        <div>
            <span>年龄:</span>
            <input type="number" v-model="age" />
        </div>
        <div>
            <span>性别:</span>
            <select v-model="sex">
                <option value=""></option>
                <option value="男">男</option>
                <option value="女">女</option>
            </select>
        </div>
        <div>
            <button @click="add">添加/修改</button>
        </div>
        <div>
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(item,index) in stuList" :key="item.id">
                    <td>{{ index + 1 }}</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.age }}</td>
                    <td>{{ item.sex }}</td>
                    <td>
                        <button @click="del(item.id)">删除</button>
                        <button @click="edit(item.id)">编辑</button>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                stuList: [
                    { id: 1, name: '张三', age: 18, sex: '男' },
                    { id: 2, name: '李四', age: 20, sex: '女' },
                    { id: 3, name: '王五', age: 19, sex: '女' },
                ],
                name: '',
                age: '',
                sex: '',
                id: '',
                flag: false,
            },
            methods: {
                add() {
                    if (this.flag) {
                        // this.stuList[0].name = '大剩'
                        this.flag = false
                        if (!this.name || !this.age || !this.sex) {
                            return alert('不能填入空数据')
                        }
                        const index = this.stuList.findIndex(item => item.id === this.id)
                        // this.stuList[index].id = this.id
                        // this.stuList[index].name = this.name
                        // this.stuList[index].age = this.age
                        // this.stuList[index].sex = this.sex
                        Object.keys(this.stuList[index]).forEach(key => this.stuList[index][key] = this[key])

                    }
                    else {
                        if (!this.name || !this.age || !this.sex) {
                            return alert('不能填入空数据')
                        }
                        this.stuList.push({
                            id: this.stuList.length + 1,
                            name: this.name,
                            age: this.age,
                            sex: this.sex
                        })
                    }
                },
                edit(id) {
                    const item = this.stuList.find(item => item.id === id)
                    this.name = item.name
                    this.age = item.age
                    this.sex = item.sex
                    this.id = item.id
                    this.flag = true
                },
                del(id) {
                    this.stuList = this.stuList.filter(item => item.id !== id)
                }
            }
        })
    </script>
</body>

</html>